<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Creditcate;
use app\admin\model\Creditjob;
use app\admin\model\Xinyong;
use app\admin\model\Credittype;
use app\admin\model\Permission;
use app\util\Util;
use app\util\File;

/*
 * 信用贷
 */
class CredittypeController extends CommonController
{
    /*
     * 信用贷列表
     */
    public function index()
    {
        $id = input('param.id');
        $list = Xinyong::with('creditcate,creditjob')
            ->field('description, img', true)
            ->where('credittype','=',$id)
            ->order('add_time desc')
            ->paginate(15);

        $credittypeList = Credittype::field('id,name')->select();
        return view('index', [
            'list' => $list,
            'credittypeList' => $credittypeList,
            'ctype' => $id,
        ]);
    }

    /*
     * 添加信用贷
     */
    public function add()
    {
        $typeid = input('param.id');
        $creditcateList = Creditcate::order('id asc')->field('id, name')->select();

        $creditjobList = Creditjob::order('id asc')->field('id, name')->select();

        $credittypeList = Credittype::field('id,name')->select();
        return view('add', [
            'creditcateList' => $creditcateList,
            'creditjobList' => $creditjobList,
            'credittypeList' => $credittypeList,
            'typeid' => $typeid,
        ]);
    }

    /*
     * 写入信用贷
     */
    public function postInsert()
    {
        $all = input('post.');
        $creditcate_id = $all['creditcate_id'];
        $creditjob_id = $all['creditjob_id'];
        $name = $all['name'];
        $description = $all['description'];
        $money = $all['money'];
        $month = $all['month'];
        $lixi = $all['lixi'];
        $speed = $all['speed'];
        $payment = $all['payment'];
        $img = $all['img'];
        $content = $all['content'];
        $credittype = $all['credittype'];
        
        $moneyArray = explode('-', $money);
        $money_min = $moneyArray[0];
        $money_max = $moneyArray[1];

        $monthArray = explode('-', $month);
        $month_min = $monthArray[0];
        $month_max = $monthArray[1];

        $lixiArray = explode('-', $lixi);
        $lixi_min = $lixiArray[0];
        $lixi_max = $lixiArray[1];

        $speedArray = explode('-', $speed);
        $speed_min = $speedArray[0];
        $speed_max = $speedArray[1];
        $data = [
            'creditcate_id' => $creditcate_id,
            'creditjob_id' => $creditjob_id,
            'name' => $name,
            'description' => $description,
            'money_min' => $money_min,
            'money_max' => $money_max,
            'month_min' => $month_min,
            'month_max' => $month_max,
            'lixi_min' => $lixi_min,
            'lixi_max' => $lixi_max,
            'speed_min' => $speed_min,
            'speed_max' => $speed_max,
            'img' => $img,
            'payment' => $payment,
            'content' => $content,
            'add_time' => time(),
            'credittype' => $credittype,
        ];

        $result = Xinyong::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑信用贷
     */
    public function edit()
    {
        $id = input('param.id');

        $row = Xinyong::with('creditcate,creditjob')->where('id', '=', $id)->find();

        $creditcateList = Creditcate::order('id asc')->field('id, name')->select();

        $creditjobList = Creditjob::order('id asc')->field('id, name')->select();

        $credittypeList = Credittype::field('id,name')->select();

        return view('edit', [
            'id' => $id,
            'row' => $row,
            'creditcateList' => $creditcateList,
            'creditjobList' => $creditjobList,
            'credittypeList' => $credittypeList,
        ]);
    }

    /*
     * 保存信用贷
     */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $creditcate_id = $all['creditcate_id'];
        $creditjob_id = $all['creditjob_id'];
        $name = $all['name'];
        $description = $all['description'];
        $money = $all['money'];
        $month = $all['month'];
        $lixi = $all['lixi'];
        $speed = $all['speed'];
        $payment = $all['payment'];
        $img = $all['img'];
        $content = $all['content'];

        $moneyArray = explode('-', $money);
        $money_min = $moneyArray[0];
        $money_max = $moneyArray[1];

        $monthArray = explode('-', $month);
        $month_min = $monthArray[0];
        $month_max = $monthArray[1];

        $lixiArray = explode('-', $lixi);
        $lixi_min = $lixiArray[0];
        $lixi_max = $lixiArray[1];

        $speedArray = explode('-', $speed);
        $speed_min = $speedArray[0];
        $speed_max = $speedArray[1];

        $data = [
            'creditcate_id' => $creditcate_id,
            'creditjob_id' => $creditjob_id,
            'name' => $name,
            'description' => $description,
            'money_min' => $money_min,
            'money_max' => $money_max,
            'month_min' => $month_min,
            'month_max' => $month_max,
            'lixi_min' => $lixi_min,
            'lixi_max' => $lixi_max,
            'speed_min' => $speed_min,
            'speed_max' => $speed_max,
            'payment' => $payment,
            'content' => $content,
            'add_time' => time(),
        ];

        $row = null;
        if ($img != '') {
            $row = Xinyong::where('id', '=', $id)->field('img')->find();

            $data['img'] = $img;
        }

        $result = Xinyong::where('id','=', $id)->update($data);
        if ($result) {
            if ($img != '') {
                $file = new File();
                $file->deleteImage($row['img']);
            }

            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除信用贷
     */
    public function del()
    {
        $id = input('param.id');

        $row = Xinyong::where('id', '=', $id)->field('img')->find();

        $result = Xinyong::where('id', '=', $id)->delete();
        if ($result) {
            $file = new File();
            $file->deleteImage($row['img']);

            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }
    //类型增加
    public function index1()
    {
        $credittypeList = Credittype::field('id,name')->select();
        return view('index1', [
            'credittypeList' => $credittypeList,
        ]);
    }
    //类型修改
    public function edit1()
    {
        $id = input('param.id');
        $credittypeList = Credittype::field('id,name')
            ->where('id','=',$id)
            ->find();
        return view('edit1', [
            'credittypeList' => $credittypeList,
        ]);
    }
    /*
    * 修改
    */
    public function postSave1()
    {
        $all = input('post.');

        $id = $all['id'];
        $username = $all['name'];


        $exist = Credittype::where('name', '=', $username)->where('id', '<>', $id)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '角色已存在', null);
        }

        $data = [
            'name' => $username,
        ];



        $result = Credittype::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
    * 删除
    */
    public function del1()
    {
        $id = input('get.id');

        $exist1 = Credittype::where('id', '=', $id)->find();

        $premisson='admin@credittype@index?id='.$exist1->id;

        $result1 = Permission::where('premisson',$premisson)->delete();

        $result = Credittype::where('id', '=', $id)->delete();
        if ($result) {
            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }
       /*
  * 添加
  */
    public function add1()
    {
        return view('add1');
    }

        /*
    * 添加
    */
    public function postInsert1()
    {
        $all = input('post.');

        $username = $all['name'];


        $exist = Credittype::where('name', '=', $username)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '名称已存在', null);
        }

        $salt = Util::generateSalt();


        $data = [
            'name' => $username,
        ];
        $result = Credittype::insert($data);


        $exist1 = Credittype::where('name', '=', $username)->find();

        $data1 = [
            'name' => $username,
            'premisson' => 'admin@credittype@index?id='.$exist1->id,
            'parent_id' => 3,
        ];

        $result1 = Permission::insert($data1);


        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }
}